//
//  CarsTBViewController.m
//  HonlivApp
//
//  Created by admin on 2017/6/26.
//  Copyright © 2017年 honliv. All rights reserved.
//

#import "CarsTBViewController.h"
#import "NSString+Extension.h"
#import "CarsViewModel.h"
#import "CarsGroupModel.h"
#import "CarsModel.h"
#import "CarsCell.h"
#import "CarsHeaderView.h"

@interface CarsTBViewController ()

@property (nonatomic,strong) CarsViewModel *viewModel;

@end

@implementation CarsTBViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"出差车辆";

    self.tableView.contentInset = UIEdgeInsetsZero;
}

-(void)getDatasArray{
    [self getDatasArray:^(GetDatas handler) {
        _viewModel = [[CarsViewModel alloc] init];
        [_viewModel getDatasArraySuccess:^{
            handler(_viewModel.datasArray);
        }];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    CarsGroupModel *model = _viewModel.datasArray[section];
    return model.isShow? 1:0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 60;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    CarsGroupModel *group = _viewModel.datasArray[section];
    CGRect frame = CGRectMake(0, 0, kScreenW, 60);
    CarsHeaderView *view = [[CarsHeaderView alloc] initWithFrame:frame ];
    view.group = group;
    [view viewClicked:^{
        group.isShow = !group.isShow;
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
    }];
    
    return view;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CarsCell *cell = [CarsCell cellWithTableView:tableView];
    CarsGroupModel *model = _viewModel.datasArray[indexPath.section];
    cell.carsModel = model.carsModel;
    return cell;
}



@end
